package com.njmaomao.gj.pyot.callback;

import com.njmaomao.gj.pyot.common.ResponseCode;
import com.njmaomao.gj.pyot.common.XmlUtil;
import com.njmaomao.gj.pyot.common.exception.SystemBizFail;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.PayNotification;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.WechatNotificationResp;
import com.njmaomao.gj.pyot.service.WechatNotificationAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Api(description = "回调")
@RestController
@RequestMapping("/payment-service/wechat")
public class WechatNotificationHandler {


    private Logger logger = LoggerFactory.getLogger(WechatNotificationHandler.class);

    @Autowired
    private WechatNotificationAppService notificationService;

    @ApiOperation(value = "微信支付结果通知接口",notes = "支付结果通知接口")
    @ApiImplicitParam(name = "xmlBody",value = "微信支付结果回调的xml消息体",required = true)
    @ApiResponse(code = 200,message = "<xml>\n" +
            "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
            "  <return_msg><![CDATA[OK]]></return_msg>\n" +
            "</xml>")
    @RequestMapping(value = "/pay-notify",method = {POST})
    public String payNotify(@RequestBody String xmlBody){

        logger.info("微信支付结果通知:{}",xmlBody.replaceAll("(\n)|(\\s+)", ""));
        PayNotification payNotification = XmlUtil.xml2Bean(xmlBody, PayNotification.class);
        SystemBizFail.whenIf(!notificationService.checkSign(xmlBody, payNotification.getAppid(), payNotification.getMchId()), ResponseCode.PAYTO.INVALID_SIGN);
        try {
            notificationService.handlePayNotification(payNotification);
        }catch (Exception e){
            logger.error("微信支付结果通知处理异常",e);
            return WechatNotificationResp.fail("微信支付结果通知处理异常").toXml();
        }
        return WechatNotificationResp.success().toXml();
    }

    @ApiOperation(value = "微信退款结果通知接口",notes = "退款结果通知接口")
    @ApiImplicitParam(name = "xmlBody",value = "微信退款结果回调的xml消息体")
    @ApiResponse(code = 200,message = "<xml> \n" +
            "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
            "  <return_msg><![CDATA[OK]]></return_msg>\n" +
            "</xml>")
    @RequestMapping(value = "/refund-notify",method = {POST})
    public String refundNotify(@RequestBody String xmlBody){

        logger.debug("微信退款结果通知:{}",xmlBody.replaceAll("(\n)|(\\s+)", ""));

        Map<String, String> map = XmlUtil.xmltoMap(xmlBody);
        String encryptData = map.get("req_info");
        String appid = map.get("appid");
        String mchId = map.get("mch_id");
        try {
            notificationService.handleRefundNotification(xmlBody, appid, mchId, encryptData);
        }catch (Exception e){
            logger.error("微信退款结果通知处理异常",e);
            return WechatNotificationResp.fail("微信退款结果通知处理异常").toXml();
        }
        return WechatNotificationResp.success().toXml();
    }

}
